# Maintainer: Daniel Wallace <danielwallace at gtmanfred dot com>
# Contributor: Chris Brannon <cmbrannon79@gmail.com>
# Contributor: Geoffroy Carrier <geoffroy.carrier@aur.archlinux.org>
# Contributor: Richard Murri <admin@richardmurri.com>
pkgname=jython
pkgver=2.7.3
pkgrel=2
pkgdesc="An implementation of the Python language written in Java"
arch=('any')
url="https://www.jython.org/"
license=('PSF' 'APACHE' 'custom')
# JDK >= 9 leads to "illegal reflective access" https://bugs.jython.org/issue2656
depends=('java-environment=8' 'bash' 'python')
backup=('opt/jython/registry')
options=('!emptydirs')
install='jython.install'
source=(https://search.maven.org/remotecontent?filepath=org/python/$pkgname-installer/$pkgver/$pkgname-installer-$pkgver.jar{,.asc}
        'README.ArchLinux'
        'jython.sh')
validpgpkeys=(
  '1AC6FA76887C81B0A870FFA03979A71621665974' # Frank Wierzbicki
  'C8C4B9DC1E031F788B12882B875C3EF9DC4638E3' # Jeff Allen
)
noextract=(${pkgname}_installer-$pkgver.jar)

sha512sums=('1e43cd81809115784fb989f5dcb3e5443d1b5e0703440e1a6df7d930650e09fc77c942fa0148e247f676d301313f3d2bd94c0d61c9afccd1e23d3c59aef65f21'
            'SKIP'
            '0ab0237d456f859b2cbb0739264bb978ad98b2c52153e89fc131aa60edf7837f41617c0e53cbde05688dfc0d6fb08f1d02ebbd41da99b07af72e34a11a83f0b9'
            'c1aaf79f1cfba165fd4a03be016ac8afcc5024972a9626060f57ced0bf4dad93ccdd9ac303bdc8a5ed6b9bcb3307d152dfff2682308c740b6d2db44c2cbaf1b4')

package() {
    java -jar jython-installer-$pkgver.jar -s -t standard -d "$pkgdir"/opt/$pkgname

    pushd "$pkgdir"/opt/$pkgname/bin

    # Fix shebangs and avoid conflicts between jython and python2-{setuptools,pip}
    # https://bugs.archlinux.org/task/45315
    rm -v easy_install pip pip2
    for prog in easy_install-2.7 pip2.7 ; do
      sed -i "s#$pkgdir##" "$prog"
    done
    mv -v easy_install-2.7 easy_install-jython
    mv -v pip2.7 pip-jython

    popd

    # Fix permissions
    find "$pkgdir" -exec chmod go+r {} \;

    install -m644 -D "$srcdir"/jython.sh "$pkgdir"/etc/profile.d/jython.sh
    install -m644 -D "$srcdir"/README.ArchLinux "$pkgdir"/usr/share/doc/$pkgname/README.ArchLinux

    install -m644 -D "$pkgdir"/opt/$pkgname/LICENSE.txt "$pkgdir"/usr/share/licenses/$pkgname/LICENSE.txt
}
